package cn.kgc.springmvcday04.handler;

import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;

/**
 * @author 课工场
 * @date 2023/10/18
 * @description   全局异常处理类
 * springmvc框架中提供异常的全局处理方案
 * @RestControllerAdvice = @ControllerAdvice +@ResponseBody
 *
 */
@ControllerAdvice    // 标识当前类是全局异常处理类
//@ResponseBody      //将注解加载类上   后续的响应全部以json格式进行处理
//@RestControllerAdvice
public class GlobalExceptionHandler {


    @ExceptionHandler(NullPointerException.class)
    public String  nullPointMethod(NullPointerException e){

        e.printStackTrace();

        return "500";
    }


    @ExceptionHandler(ArithmeticException.class)
    @ResponseBody
    public HashMap arithmeticMethod(ArithmeticException e){
        e.printStackTrace();
        HashMap<String, Object> result = new HashMap<String, Object>();

        result.put("code",500);
        result.put("msg","服务器繁忙");

        return result;
    }


}
